package org.jeecg.modules.friends.base;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;


import org.jeecg.modules.friends.service.req.BasePageReq;

import com.google.common.collect.Lists;

import cn.hutool.core.collection.CollectionUtil;
import cn.org.atool.fluent.mybatis.model.StdPagedList;

/**
 * @author
 * Created on 2022-02-25
 */
public class PageUtil {
    public static <IN, OUT> PageRsp<OUT> transfer(BasePageReq basePage, StdPagedList<IN> pageList,
            Function<IN, OUT> transferFunction) {
        PageRsp<OUT> pageRsp = new PageRsp<>();
        pageRsp.setPageNum(basePage.getPageNo());
        pageRsp.setPageSize(basePage.getPageSize());
        pageRsp.setTotal(pageList == null ? 0 : pageList.getTotal());

        if (pageList == null || CollectionUtil.isEmpty(pageList.getData())) {
            pageRsp.setData(Lists.newArrayList());
            return pageRsp;
        }
        List<OUT> resultRsp = pageList.getData()
                .stream()
                .map(transferFunction)
                .collect(Collectors.toList());
        pageRsp.setData(resultRsp);
        return pageRsp;
    }

    public static <IN, OUT> HttpReturn<PageRsp<OUT>> transferPageHttpReturn(BasePageReq basePage,
            StdPagedList<IN> pageList,
            Function<IN, OUT> transferFunction) {
        PageRsp<OUT> pageRsp = new PageRsp<>();
        pageRsp.setPageNum(basePage.getPageNo());
        pageRsp.setPageSize(basePage.getPageSize());
        pageRsp.setTotal(pageList == null ? 0 : pageList.getTotal());

        if (pageList == null || CollectionUtil.isEmpty(pageList.getData())) {
            pageRsp.setData(Lists.newArrayList());
            return HttpReturn.successPageResult(pageRsp);
        }
        List<OUT> resultRsp = pageList.getData()
                .stream()
                .map(transferFunction)
                .collect(Collectors.toList());
        pageRsp.setData(resultRsp);
        return HttpReturn.successPageResult(pageRsp);
    }
}
